package megrez.oss.minio.web;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import megrez.boot.autoconfigure.minio.MinioBucketClient;
import megrez.oss.minio.web.vo.BucketVO;

/**
 * 桶接口
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@RestController
@RequestMapping("/v1/buckets")
public class BucketResource {
    @Autowired
    private MinioBucketClient bucketClient;

    /**
     * 创建桶
     * 
     * @param name
     */
    @PostMapping()
    @ResponseStatus(HttpStatus.CREATED)
    public void make(@RequestBody String name) {
        if (!bucketClient.bucketExists(name)) {
            bucketClient.makeBucket(name);
        }
    }

    @GetMapping("/list")
    public List<BucketVO> list() {
        return bucketClient.listBuckets().stream().map(BucketVO::of).collect(Collectors.toList());
    }

}
